﻿using System.Collections.Generic;
using System.IO;
using System.Xml.XPath;
using Rodunu.Movies.Services.Models.Netflix;
using Rodunu.Movies.Services.NetFlix.DataProvider.NFService;

namespace Rodunu.Movies.Services.NetFlix.DataProvider
{
    public static class CatalogDal
    {
        //#region Public Methods
        //public static CatalogList SearchTitles(INetflixService service, string term, int startIndex, int maxResults)
        //{
        //    return (CatalogList)SerializationHelper.FromXml(typeof(CatalogList), service.SearchTitles(term, startIndex, maxResults));
        //}

        //public static CatalogTitle GetTitleDetails(INetflixService service, Item item)
        //{
        //    return (CatalogTitle)SerializationHelper.FromXml(typeof(CatalogTitle), service.GetTitleDetails(item.TitleRef));
        //}

        //public static CatalogList GetSimilars(INetflixService service, Item item, int startIndex, int maxResults)
        //{
        //    if (item.Similars != null && !string.IsNullOrEmpty(item.Similars.Href))
        //        return (CatalogList)SerializationHelper.FromXml(typeof(CatalogList), service.GetSimilars(item.Similars.Href, startIndex, maxResults));
        //    return new CatalogList();
        //}

        //public static CatalogList GetSeries(INetflixService service, Item item, int startIndex, int maxResults)
        //{
        //    if (item.Series != null && !string.IsNullOrEmpty(item.Series.Href))
        //        return (CatalogList)SerializationHelper.FromXml(typeof(CatalogList), service.GetSeries(item.Series.Href, startIndex, maxResults));
        //    return new CatalogList();
        //}

        //public static CatalogList GetEpisodes(INetflixService service, Item item, int startIndex, int maxResults)
        //{
        //    if (item.Episodes != null && !string.IsNullOrEmpty(item.Episodes.Href))
        //        return CheckCanWatchNow(service, (CatalogList)SerializationHelper.FromXml(typeof(CatalogList), service.GetEpisodes(item.Episodes.Href, startIndex, maxResults)));
        //    return new CatalogList();
        //}

        //public static string GetSynopsis(INetflixService service, Item title)
        //{
        //    if (title.Synopsis == null) return null;
        //    return GetGenericResource(service, title.Synopsis.Href, "/synopsis/text()");
        //}

        //public static List<Person> GetCast(INetflixService service, Item title)
        //{
        //    return GetPersonList(service, title.Cast);
        //}

        //public static List<Person> GetDirectors(INetflixService service, Item title)
        //{
        //    return GetPersonList(service, title.Directors);
        //}

        //public static List<FormatAvailability> GetFormats(INetflixService service, Item title)
        //{
        //    if (title.Formats == null) return null;
        //    return (List<FormatAvailability>)SerializationHelper.FromXml(typeof(FormatAvailabilityList), service.GetGenericResource(title.Formats.Href));
        //}

        //public static Filmography GetFilmography(INetflixService service, Person person)
        //{
        //    if (person.Filmography == null) return null;
        //    return (Filmography)SerializationHelper.FromXml(typeof(Filmography), service.GetGenericResource(person.Filmography.Href));
        //}

        //public static List<Award> GetAwards(INetflixService service, Item title)
        //{
        //    if (title.Awards == null) return null;
        //    return (List<Award>)SerializationHelper.FromXml(typeof(AwardList), service.GetGenericResource(title.Awards.Href));
        //}

        //public static Person GetPerson(INetflixService service, Link personLink)
        //{
        //    return (Person)SerializationHelper.FromXml(typeof(Person), service.GetGenericResource(personLink.Href));
        //}
        //#endregion

        //#region Private Methods
        //private static List<Person> GetPersonList(INetflixService service, Link link)
        //{
        //    if (link == null) return null;
        //    return (List<Person>)SerializationHelper.FromXml(typeof(PersonList), service.GetGenericResource(link.Href));
        //}

        //private static string GetGenericResource(INetflixService service, string uri, string xpath)
        //{
        //    string xml = service.GetGenericResource(uri);
        //    XPathNavigator doc = new XPathDocument(new StringReader(xml)).CreateNavigator();
        //    return doc.SelectSingleNode(xpath).Value;
        //}
        //private static CatalogList CheckCanWatchNow(INetflixService service, CatalogList items)
        //{
        //    foreach (var title in items.Titles)
        //    {
        //        //service.get
        //    }
        //    return items;
        //}
        //#endregion
    }
}